package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.Soft;
import cn.nubia.admin.application.bean.SoftRecommend;
import cn.nubia.admin.application.bean.SoftRecommendVo;
import cn.nubia.admin.application.query.SoftRecommendQuery;
import cn.nubia.admin.application.service.SoftRecommendService;
import cn.nubia.admin.application.service.SoftService;
import cn.nubia.admin.application.service.TerminalService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.service.DictFieldItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by cnJason on 2015/7/10.
 * 软件推荐控制层.
 */
@Controller
@RequestMapping(value = "/admin/soft")
public class SoftRecommendController {


    @Autowired
    private SoftRecommendService softRecommendService;

    @Autowired
    private SoftService softService;
    @Autowired
    DictFieldItemService dictFieldItemService;
    @Autowired
    TerminalService terminalService;
    /**
     * 保存推荐.
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveRecommend", method = RequestMethod.POST)
    public Response<Boolean> saveRecommend(SoftRecommend softRecommend) {
        softRecommendService.save(softRecommend);
        return Response.getSuccessResponse(true);
    }

    /**
     * 查看推荐.
     *
     * @return
     */
    @RequestMapping(value = "viewRecommend", method = RequestMethod.GET)
    public ModelAndView viewRecommend(Integer softId) {
        ModelAndView modelAndView = new ModelAndView("/app/recommendForm");
        SoftRecommend softRecommend = null;
        Soft soft = new Soft();
        if (softId != null) {
            soft = softService.get(softId);
            softRecommend = softRecommendService.getRecommendBySoftId(softId);
            if(softRecommend ==null) softRecommend = new SoftRecommend();
        }
        modelAndView.addObject("soft", soft);
        modelAndView.addObject("softRecommend", softRecommend);
        modelAndView.addObject("recommendTypes", dictFieldItemService.listItemsByDictName("recommendType"));//
        modelAndView.addObject("terminals",terminalService.listTerminals());
        return modelAndView;
    }
    @RequiresPermissions(value = "soft:rec:recommendList")
    @RequestMapping(value = "recommendList")
    public ModelAndView recommendList(SoftRecommendQuery softRecommendQuery) {
        ModelAndView modelAndView = new ModelAndView("/app/recommendList");
        Paged<SoftRecommendVo> softRecommends = softRecommendService.findPagedList(softRecommendQuery);

        modelAndView.addObject("query", softRecommendQuery);
        modelAndView.addObject("softRecommends", softRecommends);

        return modelAndView;
    }

    @RequestMapping(value = "delSoftRecommend")
    public ModelAndView delSoftRecommend(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/app/recommendList");
        softRecommendService.deleteSoftRecommend(id);
        return modelAndView;
    }


}
